package sweet.helper

import java.io._
import java.{ util=> jutil }
import scala.collection._

// === scala to java AND java collection enhancement

//conver s.iterator to jcollection
class JCollection[A](itr: scala.Iterator[A]) extends jutil.AbstractCollection[A]{ 
  def iterator = new jutil.Iterator[A]{
    def hasNext = itr.hasNext
    def next = itr.next
    def remove = throw new UnsupportedOperationException("remove")
  }
  def size = itr.toList.size  
} 

//conver s.seq to jlist
class JList[A](seq: scala.Seq[A]) extends jutil.AbstractList[A] {
  private lazy val seqSize = seq.size
  def get(idx: Int) = seq(idx)
  def size = seqSize
}

//jmap enhancement.
object JMap{
  def apply[A, B](pairs: (A,B)*): JMap[A,B] = {
    val m = new JMap[A,B]
    for((k,v) <- pairs) m.put(k,v)
    m
  }
}
class JMap[A,B] extends jutil.HashMap[A,B]

//jproperties enhancement.
object JProperties{
  def apply() = new jutil.Properties
  
  def apply(name: String): jutil.Properties = apply(new File(name))
  def apply(file: File): jutil.Properties = {
    val prop = new jutil.Properties
    prop.load(new FileReader(file))
    prop
  }
}

/*
class JMap[A](map: scala.collection.Map[A, B]) extends jutil.AbstractMap[A, B] {
  def entrySet: jutilSet = {
    new JCollection(map
  }
}*/

// === java to scala
class SProperties(props: jutil.Properties) 
  extends jcl.Hashtable[String,String](props.asInstanceOf[jutil.Hashtable[String,String]])

class SStringMap(map: jutil.Map[String,String]) extends jcl.MapWrapper[String,String]{
  def underlying = map
}
